﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;

namespace JKZP
{
    public partial class CameraForm : Form
    {
        public CameraForm()
        {
            InitializeComponent();
        }

        private void CameraForm_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            if (PP.gRoadList != null)
            {
                foreach (PP.RoadParam road in PP.gRoadList)
                {
                    foreach (JKCamera cam in road.JKCamList)
                    {
                        dataGridView1.Rows.Add(new object[] { road.LKID, road.LKMC, cam.m_CameraName, cam.m_IP, cam.m_Type, cam.m_Position, cam.m_bz, cam.m_deviceID, cam.m_UserName, cam.m_PassWord });
                    }
                }
            }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            addcamera form_addcam = new addcamera();
            form_addcam.Owner = this;
            form_addcam.ShowDialog();
            form_addcam.Dispose();
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要修改的记录");
            }
            else
            {
                editcamera form_editcam = new editcamera();
                form_editcam.Owner = this;
                form_editcam.rowindex = this.dataGridView1.SelectedRows[0].Index;
                form_editcam.roadid = this.dataGridView1.SelectedRows[0].Cells["roadid"].Value.ToString();
                form_editcam.camroad = this.dataGridView1.SelectedRows[0].Cells["camroad"].Value.ToString();
                form_editcam.camip = this.dataGridView1.SelectedRows[0].Cells["camip"].Value.ToString();
                form_editcam.camtype = this.dataGridView1.SelectedRows[0].Cells["camtype"].Value.ToString();
                form_editcam.camname = this.dataGridView1.SelectedRows[0].Cells["camname"].Value.ToString();
                form_editcam.camposition = this.dataGridView1.SelectedRows[0].Cells["camposition"].Value.ToString();
                form_editcam.cambz = this.dataGridView1.SelectedRows[0].Cells["cambz"].Value.ToString();
                form_editcam.camdeviceid = this.dataGridView1.SelectedRows[0].Cells["deviceid"].Value.ToString();
                form_editcam.username = this.dataGridView1.SelectedRows[0].Cells["username"].Value.ToString();
                form_editcam.password = this.dataGridView1.SelectedRows[0].Cells["password"].Value.ToString();
                form_editcam.ShowDialog();
                form_editcam.Dispose();
            }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要删除的记录");
            }
            else
            {
                if (DeleteCam(this.dataGridView1.SelectedRows[0].Cells["camip"].Value.ToString()) == true)
                {
                    this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                    ((Form_Main)this.Owner).UpDateTree();

                    if (PP.gISWebVersion == false)
                    {
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        if (PP.kdws.JKZP_UploadRoad(File.ReadAllText(PP.gRoadFile)) == true)
                        {
                            PP.ReLoad();
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool DeleteCam(string camip)
        {
            //<?xml version="1.0" encoding="UTF-8"?>
            //<param>
            //<road roadid="01" gbdm="123456789" roadname="测试路口一">
            //    <camera ip="172.16.22.252" type="HK9121" direction="由东向西" workmode="" bz=""></camera>
            //    <camera ip="172.16.22.250" type="HK9151" direction="由南向北" workmode="" bz=""></camera>
            //</road>
            //</param>
            string xpath = "descendant::camera[@ip='" + camip + "']";
            bool ans = PP.DelConfig(PP.gRoadFile, xpath, false);
            if (ans == true)
            {
                PP.DelCam(camip);
            }
            return ans;
        }
    }
}
